package net.liuyimin.controller;

import net.liuyimin.service.StockService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
public class StockController {

    @Resource
    private StockService stockService;

    /**
     * 演示超卖现象
     */
    @GetMapping("stock/deduct")
    public String deduct(){
        stockService.deduct();
        return "hello stock deduct！！";
    }

    /**
     * JVM本地锁 : 使用 synchronized 关键字解决超卖现象
     */
    @GetMapping("stock/deductBySynchronized")
    public String deductBySynchronized(){
        stockService.deductBySynchronized();
        return "hello stock deductBySynchronized！！";
    }

    /**
     * JVM本地锁 : 使用 ReentrantLock 可重入锁解决超卖现象
     */
    @GetMapping("stock/deductByReentrantLock")
    public String deductByReentrantLock(){
        stockService.deductByReentrantLock();
        return "hello stock deductByReentrantLock！！";
    }

    /**
     * MySQL锁机制 : 使用一条SQL语句解决超卖问题
     */
    @GetMapping("stock/deductByOneSql")
    public String deductByOneSql(){
        stockService.deductByOneSql();
        return "hello stock deductByOneSql！！";
    }

    /**
     * MySQL锁机制 : 使用SelectForUpdate悲观锁解决超卖问题
     */
    @GetMapping("stock/deductBySelectForUpdate")
    public String deductBySelectForUpdate(){
        stockService.deductBySelectForUpdate();
        return "hello stock deductBySelectForUpdate！！";
    }

    /**
     * MySQL锁机制 : 借助version乐观锁解决超卖问题
     */
    @GetMapping("stock/deductByCasVersion")
    public String deductByCasVersion(){
        stockService.deductByCasVersion();
        return "hello stock deductByCasVersion！！";
    }

}
